package com.cloud.controller;

import com.cloud.common.result.Result;
import com.cloud.common.result.ResultCode;
import com.cloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 订单表 前端控制器
 *
 * @author Sailer Wen
 * @since 2024-07-22
 */
@RestController
@RequestMapping("/consumer")
// hystrix 全局fallback方法
@DefaultProperties(defaultFallback = "globalFallback")
public class OrderHystrixController {
    public Result globalFallback()
    {
        return Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR,"Global异常处理信息，请稍后再试!");
    }

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping(value = "/paymentDefault/hystrix/ok/{id}")
    @HystrixCommand
    public Result paymentFeignOK(@PathVariable("id") Integer id) {
        return paymentHystrixService.paymentFeignOK(id);
    }

    @GetMapping(value = "/paymentDefault/hystrix/timeout/{timeout}")
    @HystrixCommand
    public Result paymentFeignTimeout(@PathVariable("timeout") Long timeout) {
        // OpenFeign客户端一般默认等待1秒钟
        return paymentHystrixService.paymentFeignTimeout(timeout);
    }

    @GetMapping(value = "/paymentDefault/hystrix/timeout2/{timeout}")
    @HystrixCommand
    public Result paymentFeignTimeout2(@PathVariable("timeout") Long timeout) {
        //故意制造消费者端的异常
        int age = 10 / 0;
        return paymentHystrixService.paymentFeignTimeout(timeout);
    }

}